<?php
namespace frontend\modules\Oauth\controllers;

use Yii;
use frontend\controllers\RestController;
use frontend\modules\Oauth\services\UserService;
use yii\helpers\Json;
use yii\base\DynamicModel;

/**
 * Class UserController
 * @package frontend\modules\Oauth\controllers
 */
class UserController extends RestController
{
    /**
     * @return array
     * 引入行为验证 过滤授权
     */
    public function behaviors()
    {
        return [
            'partnerAccess' => [
                'class' => 'common\validators\PartnerAccessFilter',
                'denyCallback' => function ($error) {
                    return self::failureResponse(500,$error);
                },
            ]
        ];
    }

    /**
     * @throws
     * 接收ticket参数 然后调用接口拉取用户信息
     */
    public function actionInfo()
    {
        $form = DynamicModel::validateData([
            'ticket' => Yii::$app->getRequest()->get('ticket'),
        ],[
            ['ticket', 'required'],
        ]);
        if ($form->hasErrors()) {
            return self::failureResponse(500,current($form->getFirstErrors()));
        }
        // 获取用户数据
        $userData   = UserService::getCache($form->ticket);
        if(!$userData){
            return static ::failureResponse(500,'ticket已失效') ;
        }
        $userData   = Json::decode($userData);
        return self::successResponse($userData) ;
    }

}
